package edu.gatech.gro.model.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.gatech.gro.model.ItemType;

public class ItemTypeDao extends DatabaseHandler {

	private static final String TAG = "ITEM_TYPE_DAO";

	public ItemTypeDao(Context context) {
		super(context);
	}

	@Override
	public void clear() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(ITEM_TYPE_TABLE, "id > 0", null);
		db.close();
	}

	public ArrayList<ItemType> getAllItemTypes() {
		ArrayList<ItemType> list = new ArrayList<ItemType>();
		String selectQuery = "SELECT  * FROM " + ITEM_TYPE_TABLE;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				ItemType itemType = new ItemType();
				itemType.setId(cursor.getInt(0));
				itemType.setName(cursor.getString(1));
				itemType.setNameClean(cursor.getString(2));
				itemType.setCreationTime(cursor.getInt(3));
				itemType.setLastUpdateTime(cursor.getInt(4));
				itemType.setDeleteFlag(cursor.getInt(5) == 1);
				list.add(itemType);
			} while (cursor.moveToNext());
		}
		db.close();

		return list;
	}

	public int count() {
		String countQuery = "SELECT  * FROM " + ITEM_TYPE_TABLE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		db.close();
		return cursor.getCount();
	}

	public boolean saveItemType(final ItemType itemType) {
		if (itemType == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("name", itemType.getName());
		values.put("nameClean", itemType.getNameClean());
		values.put("creationTime", itemType.getCreationTime());
		values.put("lastUpdateTime", itemType.getLastUpdateTime());
		values.put("deleteFlag", itemType.isDeleteFlag() ? 1 : 0);

		long id = db.insert(ITEM_TYPE_TABLE, null, values);
		if (id == -1) {
			// TODO: error*
			return false;
		} else {
			itemType.setId((int) id);
		}

		db.close();
		return true;
	}

	public boolean upgrade(ArrayList<ItemType> itemTypes) {
		if (itemTypes == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		for (ItemType itemType : itemTypes) {
			if (itemType.isDeleteFlag()) {
				db.delete(ITEM_TYPE_TABLE, "id = ?", new String[] { String.valueOf(itemType.getId()) });
			} else {
				values.clear();
				values.put("id", itemType.getId());
				values.put("name", itemType.getName());
				values.put("nameClean", itemType.getNameClean());
				values.put("creationTime", itemType.getCreationTime());
				values.put("lastUpdateTime", itemType.getLastUpdateTime());
				values.put("deleteFlag", 0);
				db.replace(ITEM_TYPE_TABLE, null, values);
			}
		}
		db.close();

		return true;
	}

	public boolean updateItemType(final ItemType itemType) {
		if (itemType == null || itemType.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("name", itemType.getName());
		values.put("nameClean", itemType.getNameClean());
		values.put("lastUpdateTime", itemType.getLastUpdateTime());
		values.put("deleteFlag", itemType.isDeleteFlag() ? 1 : 0);

		// updating row
		int r = db.update(ITEM_TYPE_TABLE, values, "id = ?", new String[] { String.valueOf(itemType.getId()) });
		db.close();
		return r == 1;
	}

	public boolean deleteItemType(final ItemType itemType) {
		if (itemType == null || itemType.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		int r = db.delete(ITEM_TYPE_TABLE, "id = ?", new String[] { String.valueOf(itemType.getId()) });
		db.close();
		return r == 1;
	}
}
